import os
os.environ['BOTO_CONFIG'] = 'boto.cfg'
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
import os
from google.appengine.ext.webapp import template
import httplib2
import urllib
import logging
import simplejson
#from boto import boto

from wsgiref.handlers import format_date_time
from datetime import datetime
from time import mktime

class MainHandler(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'html/landing_page.html')
        self.response.out.write(template.render(path, {'mdg': 'Here is where the message goes...'}))
        return

class MusicPlayerHandler(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'html/music_player.html')
        self.response.out.write(template.render(path, {'mdg': 'Here is where the message goes...'}))
        return

class GSTestHandler(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'html/gs-test.html')
        self.response.out.write(template.render(path, {'mdg': 'Here is where the message goes...'}))
        return

class OAuthCallbackHandler(webapp.RequestHandler):
    def get(self):
        code = self.request.get('code')
        self.response.out.write('code = '+code)
        client_id = '889746893028.apps.googleusercontent.com'
        client_secret = 'f_2OLb-ROXO10JETua1qycpC'
        redirect_uri = 'http://localhost:8100/oauth2callback'
    	data = {'grant_type': 'authorization_code', 
		'client_id': client_id,
		'client_secret': client_secret,
		'redirect_uri': redirect_uri,
		'code': code }
	body = urllib.urlencode(data)
	
	h = httplib2.Http()
	resp, content = h.request('https://accounts.google.com/o/oauth2/token', method="POST", body=body, headers={'content-type':'application/x-www-form-urlencoded'})
        logging.info (content)
        obj_response = simplejson.loads(content)
        access_token = obj_response['access_token']
        refresh_token = obj_response['refresh_token']
        self.response.out.write('access_token = '+access_token)
        self.response.out.write('refresh_token = '+refresh_token)

        h = httplib2.Http()
        now = datetime.now()
        stamp = mktime(now.timetuple())
        date_string = format_date_time(stamp) #--> Wed, 22 O
        
        resp, content = h.request('https://commondatastorage.googleapis.com/leseansbucket', method="GET",
                                  headers = {'Authorization': 'OAuth '+access_token,
                                             'Date': date_string,
                                             'Host': 'commondatastorage.googleapis.com',
                                             'x-goog-api-version': '2' } )
                                             #'x-goog-project-id': '889746893028' } )

        logging.info(content)
        #return resp, content
        return

class ListFilesHandler(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'html/list_files.html')
        self.response.out.write(template.render(path, {}))
        return        

class UploadMusicHandler(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'html/upload_music.html')
        self.response.out.write(template.render(path, {}))
        return        
        
class MusicPlayerHandler(webapp.RequestHandler):
    def get(self):
        return

def main():
    application = webapp.WSGIApplication([('/', MainHandler),
                                          ('/music_player', MusicPlayerHandler),
                                          ('/gs-test', GSTestHandler),
                                          ('/oauth2callback', OAuthCallbackHandler),
                                          ('/list_files', ListFilesHandler),
                                          ('/upload_music', UploadMusicHandler)],
                                         debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
